/*
 * @(#)EncodingFilter.java
 * 
 * Project: BUS
 * 
 * Modify Information:
 * =========================================================================
 * Change History: 
 * 
 * Author     Date        Description 
 * =================================================== 
 * HZ SDC     2003-11-06  first release
 * 
 * Copyright Notice: Copyright (c) 2001-2005 Beijing HiTRUST Technology Co.,
 * Ltd. 1808 Room, Science & Technology Building, No. 9 South Street, Zhong Guan
 * Cun, Haidian District, Beijing ,100081, China All rights reserved.
 * 
 * This software is the confidential and proprietary information of HiTRUST.COM,
 * Inc. ('Confidential Information'). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with HiTRUST.
 * 
 * Warning:
 * =========================================================================
 * 
 */
package com.hitrust.gs.util.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.hitrust.gs.lib.GSLogWriter;


public class EncodingFilter implements Filter {
    private String defaultEncoding = "UTF-8";

    public void init(FilterConfig config) throws ServletException {
        if(config.getInitParameter("defaultEncoding") != null){
            this.defaultEncoding = config.getInitParameter("defaultEncoding");
        }
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException,
            ServletException {
        req.setCharacterEncoding(getEncoding());
        res.setContentType(getContentType());
        try {
            chain.doFilter(req, res);
        }
        catch (IOException e) { 
           GSLogWriter.error("===" + e.getMessage());           
            throw e;
        }

    }

    private String getEncoding() {
        return defaultEncoding;
    }

    protected String getContentType() {
        return "text/html; charset=" + getEncoding();
    }

    public void destroy() {

    }

}